<template>
  <!-- 设计图纸管理 -->
  <base-breadcrumb class="allPlan">
    <a-row :gutter="10"  class="page-height">
      <a-col :span="4" style="border-right: 1px solid #d9d9d9">
        <a-card :bordered="false"  class="page-height-tree">
          <title-name title="项目列表"></title-name>

          <!-- 公司组织树搜索三级权限 -->
          <PermissionTreeOrganization :collapsed="false" @select="handleSelect" />
          <!-- <tree-project
            ref="treeOrganization"
            @loader="search"
            :selectedKeys.sync="selectedKeys"
            :selectOptions.sync="selectOptions"
          ></tree-project> -->
        </a-card>
      </a-col>
      <a-col :span="20" class="page-height-list-wrapper">
        <searchData :searchData="searchArr" @search="search"></searchData>
        <a-card  class="page-height-list">
          <!-- <title-name title="变更列表"></title-name> -->
          <!-- 表格 -->
          <list-table
            ref="table"
            :columns="columns"
            :startIndex="1"
            :request="loadData"
            :outSelectedRowKeys.sync="selectedKeys"
            :exportConfig="{
              use: true,
              select: $route.meta.title,
              all: $route.meta.title,
              customColumns: customColumns
            }"
          >
            <!-- 新增 -->
            <a-button
              v-btnPermission="'tm_020_add_01'"
              slot="prefix"
              class="mb12"
              icon="plus"
              type="primary"
              @click="handleManage(2)"
              :disabled="selectOptions.tageCode !== 'PD'"
              >新增</a-button
            >
            <a-button
                    v-btnPermission="'tm_020_add_01'"
                    slot="prefix"
                    class="mb12"
                    icon="download"
                    type="primary"
                    :disabled="!selectedKeys.length"
                    @click="batchExportFiles"
            >批量导出附件</a-button
            >
            <span slot="operation" slot-scope="text, record">
               <MoreButton>
              <a v-btnPermission="'tm_020_loo_04'" @click="handleManage(0, record)">查看</a>
              <!-- 编辑 -->
              <span
                v-if="record.status == 'DRAFT' || record.status == 'REJECTED'"
                v-btnPermission="'tm_020_edi_02'"
              >
                <a @click="handleManage(record.status === 'PAST' ? -1 : 1, record)">编辑</a>
              </span>

              <template v-if="record.status == 'REJECTED' || record.status == 'PAST'">
                  <a
                    style="color: #1890ff;"
                    v-if="record.status == 'DRAFT' || record.status == 'REJECTED'"
                    v-btnPermission="'tm_020_del_03'"
                    @click="handleRemoveParams({ id: record.id, type: 1 })"
                    >删除
                  </a>
                  <!-- 作废 -->
                  <a
                    style="color: #1890ff;"
                    v-if="record.status == 'PAST'"
                    v-btnPermission="'tm_020_tov_05'"
                    @click="handleInvalid({ id: record.id, type: 2 })"
                  >
                    作废
                  </a>
                  <!-- 审批 -->
                  <span v-if="record.status !== 'DRAFT'" v-btnPermission="'tm_020_loo_04'">
                    <approval-button :id="record.id" :status="record.status" /> </span
                >
              </template>
              <template v-else>
                <!-- 删除 -->
                <span v-if="record.status == 'DRAFT' || record.status == 'REJECTED'" v-btnPermission="'tm_020_del_03'">
                  <a @click="handleRemoveParams({ id: record.id, type: 1 })">删除</a>
                </span>
                <!-- 审批 -->
                <span v-if="record.status !== 'DRAFT'" v-btnPermission="'tm_020_loo_04'">
                  <approval-button :id="record.id" :status="record.status" />
                </span>
              </template>
              <!-- 审批通过编辑 -->
              <!-- <span v-if="record.status === 'PAST'" v-btnPermission="'tm_020_edi_03'">
                <a-divider type="vertical" />
                <a @click="handleManage(-1, record)">编辑</a>
              </span> -->
               </MoreButton>
            </span>

            <span slot="status" slot-scope="text, record">
              <list-status :status="record.status" :isApproval="true" conversion="en" :text="text"></list-status>
            </span>
          </list-table>
        </a-card>
      </a-col>
    </a-row>
  </base-breadcrumb>
</template>

<script>
import BasePage from '@/views/basePage' // 基础配置
import { getDrawChangeList, delDrawChange } from '@/api/technical/designFM'
import { batchExportFiles } from "@/api/technology"
import TreeProject from '@/components/businessComponents/TreeProject' // 树
import Selector from '@/components/Select/Selector'
import MoreButton from '@/components/MoreButton'
import PermissionTreeOrganization from '@/components/businessComponents/PermissionTreeOrganization'
const columns = [
  {
    title: '序号',
    width: 80,
    dataIndex: 'index',
    customRender() {
      return arguments[2] + 1
    }
  },
  {
    title: '所属分公司',
    dataIndex: 'branchComName',
    ellipsis: true
  },
  {
    title: '所属项目',
    dataIndex: 'projectName',
    ellipsis: true
  },
  {
    title: '专业类别',
    dataIndex: 'professionalTypeName',
    ellipsis: true
  },
  {
    title: '工程部位',
    dataIndex: 'projectLocation',
    ellipsis: true
  },
  {
    title: '设计变更名称',
    dataIndex: 'name',
    ellipsis: true
  },
  {
    title: '设计变更编号',
    dataIndex: 'num',
    ellipsis: true
  },
  {
    title: '发文部门',
    dataIndex: 'sendDepartment',
    ellipsis: true
  },
  {
    title: '收文日期',
    dataIndex: 'receiptDate'
  },
  {
    title: '实施状态',
    dataIndex: 'drawEnumStr'
  },
  {
    title: '状态',
    dataIndex: 'status',
    scopedSlots: { customRender: 'status' }
  },
  {
    title: '操作',
    dataIndex: 'operation',
    fixed: 'right',
    width: '180px',
    scopedSlots: { customRender: 'operation' }
  }
]

import { STable } from '@/components'
import { majorTypeSelectListTree } from '@/api/technology'
import moment from 'moment'
export default {
  name: 'a' + Date.now(),
  extends: new BasePage(),
  components: {
    STable,
    TreeProject,
    MoreButton,
    PermissionTreeOrganization
  },
  data() {
    this.columns = columns

    return {
      // 统计
      statistics: {
        bluePicture: 0,
        deepPicture: 0,
        otherInteger: 0,
        totalPicture: 0,
        whitePicture: 0
      },

      selectedKeys: [],
      selectOptions: {},

      queryParam: {
        receiptBeginDate: new Date().getFullYear() + '-01' + '-01',
        receiptEndDate: this.formatDate(new Date()),
        changeReviewEnum: 'CHANGE'
      },
      removeApi: { remove: delDrawChange, invalid: delDrawChange, undo: delDrawChange }, // api

      loadData: async parameter => {
        const requestParameters = Object.assign({}, parameter, { query: this.queryParam })
        return getDrawChangeList(requestParameters).then(res => {
          return res
        })
      },

      customColumns: [
        { title: '序号', dataIndex: 'index' },
        { title: '所属分公司', dataIndex: 'branchComName' },
        { title: '所属项目', dataIndex: 'projectName' },
        { title: '专业类型', dataIndex: 'professionalTypeName' },
        { title: '设计变更名称', dataIndex: 'name' },
        { title: '设计变更编号', dataIndex: 'num' },
        { title: '发文部门', dataIndex: 'sendDepartment' },
        { title: '收文日期', dataIndex: 'receiptDate' },
        { title: '实施状态', dataIndex: 'drawEnumStr' },
        { title: '状态', dataIndex: 'statusStr' }
      ],
      searchArr: [
        // {
        //   name: '专业类别',
        //   type: Selector,
        //   valueKey: 'professionalTypeId',
        //   selectType: '9',
        //   urlType: 'technical'
        // },
        {
          name: '专业类别',
          type: 'tree-select',
          value: 'professionalTypeId',
          attrs: {
            replaceFields: {children:'children', title:'majorName', key:'id', value: 'id' },
            showSearch: true,
            treeNodeFilterProp: 'title'
          },
          data: []
        },
        {
          name: '设计变更名称',
          type: 'input',
          value: 'name'
        },
        {
          name: '设计变更编号',
          type: 'input',
          value: 'num'
        },
        {
          name: '发文部门',
          type: 'input',
          value: 'sendDepartment'
        },
        {
          name: '收文日期',
          type: 'dateRange',
          value: 'callNameDate',
          attr: {
            //搜索设置默认搜索日期
            defaultValue: [new Date().getFullYear() + '-01' + '-01', this.formatDate(new Date())],
            rangeFormat: ['receiptBeginDate', 'receiptEndDate']
          }
        },
        {
          name: '工程部位',
          type: 'input',
          value: 'projectLocation'
        },
      ]
    }
  },
created() {
    console.log(moment().format('yyyy-MM-DD HH:mm'), 'moment().format(\'yyyy-MM-DD HH:mm\')')
  this.majorTypeSelectListTree()
},
  methods: {
    batchExportFiles() {
      const data = {
        ids:this.selectedKeys,
        typeId: 0,
        urlStr: window.location.origin
      }
      batchExportFiles(data).then(res=>{
        this.handleDownload(res)
      })
    },
    /**
     * 转义下载
     * @param parameter
     */
    handleDownload(res) {
      const blob = new Blob([res.data])
      const filename = decodeURIComponent(res.headers['content-disposition'].split('filename=')[1])
      console.log('filenamefilename',filename)
      const downloadElement = document.createElement('a')
      const href = window.URL.createObjectURL(blob) // 创建下载的链接
      downloadElement.href = href
      downloadElement.download = filename.substring(filename.indexOf("''") + 1) // 获取文件名
      document.body.appendChild(downloadElement)
      downloadElement.click() //点击下载
      document.body.removeChild(downloadElement) //下载完成移除元素
      window.URL.revokeObjectURL(href) //释放blob对象
    },
    majorTypeSelectListTree() {
      majorTypeSelectListTree().then(res=>{
        res.data.forEach(item=>{
          item.majorName = item.programmeName
          item.disabled = true
        })
        this.searchArr[0].data = res.data
      })
    },
    //格式化当前日期
    formatDate(date, type = '-') {
      let M = date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1
      let D = date.getDate() < 10 ? '0' + date.getDate() : date.getDate()
      return date.getFullYear() + type + M + type + D
    },
    handleSelect(node) {
      this.selectOptions = node
      console.log(this.selectOptions, 'selectOptions')
      this.search()
    },
    //搜索
    search(value) {
      this.queryParam = {
        receiptBeginDate: new Date().getFullYear() + '-01' + '-01',
                receiptEndDate: this.formatDate(new Date()),
                changeReviewEnum: 'CHANGE'
      }
      value && (this.queryParam = { ...this.queryParam, ...value })
      console.log(this.selectOptions)
      if (this.selectOptions.tageCode == 'PD') {
        this.queryParam.projectId = this.selectOptions.projectId
      } else if (this.selectOptions.tageCode == 'BC') {
        this.queryParam.branchComId = this.selectOptions.orgId
      } else {
        delete this.queryParam.projectId
        delete this.queryParam.branchComId
      }
      this.$refs.table.refresh(true)
    },
    // 操作栏
    handleManage(type, record) {
      let query = {
        editModel: type == -1 ? 0 : type,
        comment: type == -1,
        projectId: this.selectOptions.projectId
      }
      if (record && record.id) {
        query.id = record.id
      }
      this.$router.push({
        name: 'updatedesignChanges',
        query
      })
    }
  }
}
</script>
